<?php
/**
 * 
 * 
 * $Rev: 8 $
 * $Author: thejahweh $
 * $Date: 2010-09-14 07:40:11 +0000 (Tue, 14 Sep 2010) $
 * $Id: area.class.php 8 2010-09-14 07:40:11Z thejahweh $
 * $URL: http://allone.googlecode.com/svn/trunk/plugins/smarty/area.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

class area {
	
	public			$id;
	public			$isLoaded = false;
	public static	$replace = array(
						'top'		=> 1,
						'middle'	=> 2,
						'left'		=> 3,
						'right'		=> 4,
						'bottom'	=> 5,
					);
	
	public function __construct () {
		
	}
	
	public function getArea ($AreaId) {
		$area_q = $db->query("
			SELECT
				area_id
			FROM 
				areas
			WHERE
				area_id = ".$db->quote($AreaId)."
			LIMIT 1;");
		if ($area_q !== false and $area = $area_q->fetch_assoc()) {
			$this->id		=& $area['area_id'];
			return $this->isLoaded = true;
		} else {
			u::o($db->error, 'user:getArea', 1);
			return $this->isLoaded = false;
		}
		
	}
	
	public static function getModules ($navigationnodeId, $areaPosition) {
		global $db;
		$modules_q = $db->query("
			SELECT
				*
			FROM 
				modules AS m,
				resident_modules AS a,
				areas_has_navigations_nodes AS n
			WHERE
				n.navigation_node_id = ".$db->quote($navigationnodeId)."
				AND a.area_id = ".$db->quote($areaPosition)."
				AND n.area_id = a.area_id
				AND m.module_id = a.module_id;");
		if ($modules_q !== false) {
			$modules_q->setFetchMode(PDO::FETCH_ASSOC);
			$modules = $modules_q->fetchAll();
			return $modules;
		} else {
			return false;
		}
	}
	
	public static function smarty_function_area ($params) {
		global $pluginLoader, $user;
		u::o('getArea '.$params['id'], 'smarty:area');
		if (isset($params['id'])) {
			$replace = array(
				'top'		=> 1,
				'middle'	=> 2,
				'left'		=> 3,
				'right'		=> 4,
				'bottom'	=> 5,
			);
			$areaPosition = str_replace(array_keys($replace), array_values($replace), $params['id']);
			u::o('areaPosition: '.$areaPosition, 'smarty:area', 1);
			if (is_numeric($areaPosition)) {
				$modules = area::getModules(navigation::$currentPage->id, $areaPosition);
				u::o('currentPage: '.navigation::$currentPage->id, 'smarty:area', 1);
				u::o('modules', 'smarty:area', 1);
				if (is_array($modules)) {
					/**
					 * TODO Speed?
					 */
					$aSmarty = new alloneSmarty;
					$aSmarty->addTemplateDir($pluginLoader->plugins['smarty']->dir.'/templates');
					$aSmarty->assign('areaId', $params['id']);
					$aSmarty->assign('modules', $modules);
					$aSmarty->assign('cp', navigation::$currentPage);
					$aSmarty->assign('user', $user);
					$aSmarty->display('area.tpl');
				}
			}
		}
		return '';
	}
	
	public static function smarty_function_module ($params) {
		global $pluginLoader;
		if (isset($params['module'], $params['module']['name'], $params['module']['plugin_name'], $params['module']['path'], $params['module']['class_name'])) {
			$module =& $params['module'];
			u::o($module['name'], 'smarty:area', 2);
			if ($plugin = $pluginLoader->getPlugin($module['plugin_name'])) {
				u::o($plugin->dir.'/'.$module['path'], 'smarty:area', 3);
				include_once($plugin->dir.'/'.$module['path']);
				
				$t = $pluginLoader->plugins[$module['plugin_name']];
				
				$mSmarty	= new alloneSmarty;
				$mSmarty->addTemplateDir(alloneSmarty::$templateDir.'/plugins/'.$module['plugin_name']);
				$mSmarty->addTemplateDir($t->dir.'/templates');
				
				$mClass		= new $module['class_name'];
				$mClass->setOutput($mSmarty);
				$mClass->getModule($module);
				$mClass->display();
			}
		}
		return '';
	}
	
}
?>